/**
 * Created by wangguoqing on 2017/11/8.
 */
var params=new Object();
var url=path+"/system/assignPermission/listOrgByPage";


$(function(){
    //设置AjaxLoading
    setAjaxLoading();
    var grid=$("#tt");
    initGrid(grid);
    ajaxJson(path+'/system/org/getTreeData',null,initTree);

});


//初始化树点击函数
function zTreeOnClick(event, treeId, treeNode) {
    params.orgCode = treeNode.id;
    btnQuery(false);
};


//初始化表格和数据
function initGrid(grid) {
    var columns=[
        { field: "userId", title: "ID", hidden: true, align: "left", sortable: false, resizable: true, width: 80},
        { field: "userCode", title: "用户编码", align: "left",halign: "center", sortable: false, resizable: true, width: 100},
        { field: "userName", title: "用户名称", align: "left",halign: "center", sortable: false, resizable: true, width: 120},
        { field: "organization", title: "所属部门", align: "left",halign: "center", sortable: false, resizable: true, width: 200 },
        { field: "phoneNo", title: "手机号码", align: "left",halign: "center", sortable: false, resizable: true, width: 120},
        { field: "email", title: "邮箱", align: "left",halign: "center", sortable: false, resizable: true, width: 180},
        { field: "roleDtos", title: "角色", align: "left",halign: "center", sortable: false, resizable: true, width: 400,
            formatter: function(value,row,index){
                var js = "";
                $.each(row.roleDtos,function(index,val){
                    if(index == 0){
                        js += val.roleName;
                    }else{
                        js += "," + val.roleName;
                    }
                });
                return js;
            }
        }
    ];

    initPageGrid(grid, params, url,columns,"#tb");
}

//查询按钮
function btnQuery(type){
    params.userCode = $("#userCode").val();
    var grid=$("#tt");
    getPage(grid,params,url);
    if(type){
        if(params.userCode){
            ajaxJson(path+'/system/org/getTreeDataByUserCode',params,initTree);
        }else{
            location.reload();
        }
    }
}

//分配人员
function btnAddOrganizationPersonnel() {
    layer.open({
        type: 2,
        title: '机构人员 - 添加',
        shadeClose: false,
        shade: 0.3,
        area: ['700px', '500px'],
        content: path+'/system/assignPermission/toAddOrganizationPersonnel' //iframe的url
    });
}
//分配角色
function btnAddRolePersonnel() {
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length == 0) {
        //$.messager.alert("友情提醒", "请先选择人员！", "info");
        $.alert("default", "info", "请先选择人员！");
        return;
    }
    layer.open({
        type: 2,
        title: '人员角色 - 分配',
        shadeClose: false,
        shade: 0.3,
        area: ['700px', '500px'],
        content: path+'/system/assignPermission/toAddRoleByOrgPersonal' //iframe的url
    });
}


//删除人员
function btnDelNodeByGrid(){
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length == 0) {
        //$.messager.alert("友情提醒", "请先选择人员信息！", "info");
        $.alert("default", "info", "请先选择人员！");
        return;
    }
    var rowData = rows[0];
    if(typeof(rowData.roleDtos) != "undefined" && rowData.roleDtos != null  && rowData.roleDtos != ""){
        //$.messager.alert("友情提醒", "该人员权限分配已经启用无法删除！", "info");
        $.alert("default", "info", "该人员权限分配已经启用无法删除！");
        return;
    }
    var param = new Object();
    param.userId = rowData.userId;
    param.orgCode = params.orgCode;

    $.messager.alert("问题", "请确认是否要删除？","confirm", function () {
            $.ajax({
                type:"POST",
                url:path+'/system/assignPermission/deleteByUser',
                data:param,
                dataType:"json",
                async: false,
                success:function callback(data){
                    var analyMsg = data;
                    if(analyMsg.code!="0000"){
                        //$.messager.alert('友情提醒',analyMsg.msg,'info',function(){});
                        $.alert("default", "error", analyMsg.msg);
                    }else{
                        var treeObj = $.fn.zTree.getZTreeObj("tree");
                        var node = treeObj.getNodeByParam("id", rowData.userId, null);
                        treeObj.removeNode(node);
                        btnQuery(false);
                    }
                }
            });

    });

}